package com.enation.app.shop.front.api.utils;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

@Scope("prototype")
@Controller
@RequestMapping("/app/creditintrol")
public class ImgUtil {

    public  static  String  getImg(String imgfile){
        if (imgfile==null||imgfile.length()<3){
            return "";
        }
        //int test=imgfile.indexOf("localhost");
        StringBuffer stringBuffer=new StringBuffer();
        String innetip=getInetAddress();
        //String innetip=getServerIp();
        if (imgfile.indexOf("localhost")>0){
            String ip="http://"+innetip+":";
            String img="";
            String imgtemp=imgfile.substring(17);
            img=ip+imgtemp;
            int i=1;
            return img;
        }else {
            String ip="http://"+innetip+":8086/statics";
            String img="";
            String imgtemp=imgfile.substring(3);
            img=ip+imgtemp;
            int i=1;
            return img;
        }
    }


    @RequestMapping("/addresstest")
    public static String getInetAddress(){
        try {
             InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
             String hostAddress = address.getHostAddress();//192.168.0.61
             return  hostAddress;
        } catch (Exception e) {
            e.printStackTrace();
            return "192.168.1.6";
      }
    }





//
//    // 获取服务器IP地址
//      @RequestMapping("unchecked")
//      public static String  getServerIp(){
//                String SERVER_IP = null;
//                try {
//                        Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
//                        InetAddress ip = null;
//                        while (netInterfaces.hasMoreElements()) {
//                               NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
//                                ip = (InetAddress) ni.getInetAddresses().nextElement();
//                                SERVER_IP = ip.getHostAddress();
//                                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
//                                        && ip.getHostAddress().indexOf(":") == -1) {
//                                        SERVER_IP = ip.getHostAddress();
//                                         break;
//                                    } else {
//                                         ip = null;
//                                     }
//                            }
//                    } catch (Exception e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                   }
//
//                 return SERVER_IP;
//            }

}
